// exports.Instance = void 0;
// var AL_GAME_HUA_EventConfig = require("./AL_GAME_HUA_EventConfig");
// var AL_GAME_HUA_GameConst = require("./AL_GAME_HUA_GameConst");
// var AL_GAME_HUA_GoodsConfig = require("./AL_GAME_HUA_GoodsConfig");
// var AL_GAME_HUA_CurrencyData = require("./AL_GAME_HUA_CurrencyData");
// var AL_GAME_HUA_TopBar = require("./AL_GAME_HUA_TopBar");
// var AL_GAME_HUA_AddPrefab = require("./AL_GAME_HUA_AddPrefab");
// var AL_GAME_HUA_LifeNode = require("./AL_GAME_HUA_LifeNode");
// var AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
// var AL_GAME_HUA_TimerRecord = require("./AL_GAME_HUA_TimerRecord");
// var AL_GAME_HUA_PlatformManager = require("./AL_GAME_HUA_PlatformManager");
// var AL_GAME_HUA_ConfigHandler = require("./AL_GAME_HUA_ConfigHandler");
// var AL_GAME_HUA_CustomLoading = require("../../scripts/AL_GAME_HUA_CustomLoading");
// var AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");
// (function (e) {
//     e.Game = null;
//     e.goMainCount = 0;
//     e.LevelType = AL_GAME_HUA_Define.Define.LevelType.NORMAL;
//     e.ChallengeLevel = 1;
//     e.qianwang_youxi = function (t) {
//         var n = this;
//         if (void 0 === t) {
//             t = {};
//         }
//         return new Promise(function (r) {
//             AL_GAME_HUA_CustomLoading.default.show().then(function () {
//                 cc.director.loadScene(AL_GAME_HUA_Define.Define.SCENE[2], function () {
//                     return __awaiter(n, void 0, void 0, function () {
//                         return __generator(this, function (n) {
//                             switch (n.label) {
//                                 case 0:
//                                     AL_GAME_HUA_PlatformManager.PlatformManager.getInstance().wx.triggerGC();
//                                     return e.Game
//                                         ? [
//                                               4,
//                                               new Promise(function (t) {
//                                                   e.Game
//                                                       ? e.Game.scheduleOnce(function () {
//                                                             t();
//                                                         }, void 0)
//                                                       : t();
//                                               })
//                                           ]
//                                         : [3, 3];
//                                 case 1:
//                                     n.sent();
//                                     return [4, e.Game.onPreload()];
//                                 case 2:
//                                     n.sent();
//                                     n.label = 3;
//                                 case 3:
//                                     if (t.onLaunch) {
//                                         t.onLaunch();
//                                     }
//                                     AL_GAME_HUA_CustomLoading.default.hide().then(function () {
//                                         e.Game.onPrepare();
//                                         r();
//                                     });
//                                     return [2];
//                             }
//                         });
//                     });
//                 });
//             });
//         });
//     };
//     e.jinru_zhujiemian = function (t) {
//         var n = this;
//         if (void 0 === t) {
//             t = {};
//         }
//         return new Promise(function (r) {
//             e.goMainCount++;
//             AL_GAME_HUA_PlatformManager.PlatformManager.getInstance().wx.triggerGC();
//             AL_GAME_HUA_CustomLoading.default.show().then(function () {
//                 cc.director.loadScene(AL_GAME_HUA_Define.Define.SCENE[1], function () {
//                     return __awaiter(n, void 0, void 0, function () {
//                         return __generator(this, function () {
//                             if (t.onLaunch) {
//                                 t.onLaunch();
//                             }
//                             AL_GAME_HUA_CustomLoading.default.hide().then(function () {
//                                 r();
//                             });
//                             return [2];
//                         });
//                     });
//                 });
//             });
//         });
//     };
//     e.huidao_shouye = function () {
//         AL_GAME_HUA_ConfigHandler.ConfigHandler.xiaohui_suoyou_peizhi_duixiang();
//         if (cc.find("transition")) {
//             var n = cc.find("transition");
//             cc.game.removePersistRootNode(n);
//         }
//         e.topBar.node.destroy();
//         e.topBar = null;
//         t.node.destroy();
//         t = null;
//         AL_APP_SDK_MANAGER.subGame.huidao_shouye();
//     };
//     e.jiancha_tili_shifou_zugou = function (e, t, n) {
//         if (AL_GAME_HUA_GameConst.GameConst.getInstance().vit_enabled) {
//             if (
//                 AL_GAME_HUA_CurrencyData.CurrencyData.getInstance().getHave(AL_GAME_HUA_CurrencyData.CurrencyType.VIT) > 0
//             ) {
//                 AL_GAME_HUA_CurrencyData.CurrencyData.getInstance().shiyong_huobi({
//                     type: AL_GAME_HUA_CurrencyData.CurrencyType.VIT,
//                     num: 1
//                 });
//                 AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportCost({
//                     source: AL_GAME_HUA_EventConfig.AssetsSource.game,
//                     action: AL_GAME_HUA_EventConfig.AssetsAction.cost,
//                     goodsList: [
//                         {
//                             goods: AL_GAME_HUA_GoodsConfig.GoodsId.vit,
//                             num: 1
//                         }
//                     ]
//                 });
//                 return void e();
//             }
//             AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_tishi_jiedian_mianban({
//                 str: "体力不足"
//             });
//             AL_GAME_HUA_AddPrefab.AddPrefab.create_add_vit({
//                 onBuy: t,
//                 onClose: n
//             });
//         } else {
//             e();
//         }
//     };
//     e.chuangjian_dingbu_gongju_lan = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var t;
//             var n;
//             return __generator(this, function (o) {
//                 switch (o.label) {
//                     case 0:
//                         return e.topBar && 0 != cc.isValid(e.topBar.node)
//                             ? [3, 2]
//                             : [
//                                   4,
//                                   AL_GAME_HUA_Common.Common.loadRes({
//                                       url: "prefabs/main/topBar",
//                                       bundle: AL_GAME_HUA_Define.Define.GAME_NAME,
//                                       type: cc.Prefab
//                                   })
//                               ];
//                     case 1:
//                         t = o.sent();
//                         n = cc.instantiate(t);
//                         cc.director.getScene().addChild(n, 5);
//                         e.topBar = n.getComponent(AL_GAME_HUA_TopBar.default);
//                         cc.game.addPersistRootNode(n);
//                         n.active = !1;
//                         e.topBar.connets = [];
//                         o.label = 2;
//                     case 2:
//                         return [2];
//                 }
//             });
//         });
//     };
//     e.xianshi_dingbu_tiao = function t(n) {
//         var o;
//         var i;
//         var r;
//         var s;
//         var c;
//         if (e.topBar) {
//             e.topBar.com_gold_bar.node.active = n.showGold || n.showAll;
//             e.topBar.com_star_bar.node.active = n.showStar || n.showAll;
//             e.topBar.com_vit_bar.node.active = n.showVit || n.showAll;
//             e.topBar.com_gold_bar.button.active = null === (o = n.goldButtonEnable) || void 0 === o || o;
//             e.topBar.com_vit_bar.button.active = null === (i = n.vitButtonEnable) || void 0 === i || i;
//             e.topBar.node.active = !0;
//             var l = e.topBar.connets;
//             if (-1 == l.indexOf(n.target)) {
//                 l.push(n.target);
//             }
//             if (!AL_GAME_HUA_GameConst.GameConst.getInstance().vit_enabled) {
//                 e.topBar.com_vit_bar.node.active = !1;
//             }
//             n.target.options = n;
//             r = {
//                 node: n.target,
//                 chuli_destory_hanshu: function () {
//                     l.remove(n.target);
//                     l.length > 0 ? t(l[l.length - 1].options) : (e.topBar.node.active = !1);
//                 }
//             };
//             (c = new cc.Node()).name = "XXLifeNode";
//             (s = c.addComponent(AL_GAME_HUA_LifeNode.default)).chuli_load_hanshu = r.chuli_load_hanshu;
//             s.chuli_destory_hanshu = r.chuli_destory_hanshu;
//             s.chuli_enabeld_hanshu = r.chuli_enabeld_hanshu;
//             s.chuli_disabled_hanshu = r.chuli_disabled_hanshu;
//             r.node.addChild(c);
//         }
//     };
//     var t = null;
//     e.chuangjian_jishiqi_jiluqi = function () {
//         if (!t || 0 == cc.isValid(t)) {
//             var e = new cc.Node("timerRecord");
//             var n = e.addComponent(AL_GAME_HUA_TimerRecord.default);
//             cc.director.getScene().addChild(e);
//             cc.game.addPersistRootNode(e);
//             t = n;
//         }
//     };
//     e.tianjia_jishiqi = function (e) {
//         if (t) {
//             t.tianjia_jishiqi(e);
//         }
//     };
// })(exports.Instance || (exports.Instance = {}));



// 引入外部模块
const AL_GAME_HUA_EventConfig = require("./AL_GAME_HUA_EventConfig");
const AL_GAME_HUA_GameConst = require("./AL_GAME_HUA_GameConst");
const AL_GAME_HUA_GoodsConfig = require("./AL_GAME_HUA_GoodsConfig");
const AL_GAME_HUA_CurrencyData = require("./AL_GAME_HUA_CurrencyData");
const AL_GAME_HUA_TopBar = require("./AL_GAME_HUA_TopBar");
const AL_GAME_HUA_AddPrefab = require("./AL_GAME_HUA_AddPrefab");
const AL_GAME_HUA_LifeNode = require("./AL_GAME_HUA_LifeNode");
const AL_GAME_HUA_Define = require("./AL_GAME_HUA_Define");
const AL_GAME_HUA_TimerRecord = require("./AL_GAME_HUA_TimerRecord");
const AL_GAME_HUA_PlatformManager = require("./AL_GAME_HUA_PlatformManager");
const AL_GAME_HUA_ConfigHandler = require("./AL_GAME_HUA_ConfigHandler");
const AL_GAME_HUA_CustomLoading = require("../../scripts/AL_GAME_HUA_CustomLoading");
const AL_GAME_HUA_Common = require("./AL_GAME_HUA_Common");

// 游戏实例类
(function (gameInstance) {
    // 成员变量初始化
    gameInstance.Game = null;
    gameInstance.goMainCount = 0;
    gameInstance.LevelType = AL_GAME_HUA_Define.Define.LevelType.NORMAL;
    gameInstance.ChallengeLevel = 1;

    /** 
     * 进入游戏场景方法 
     * @param {Object} options - 可选对象参数
     */
    gameInstance.qianwang_youxi = function (options = {}) {
        const _this = this;
        return new Promise((resolve) => {
            AL_GAME_HUA_CustomLoading.default.show().then(() => {
                cc.director.loadScene(AL_GAME_HUA_Define.Define.SCENE[2], async function () {
                    AL_GAME_HUA_PlatformManager.PlatformManager.getInstance().wx.triggerGC();
                    if (gameInstance.Game) {
                        await new Promise((onSchedule) => {
                            gameInstance.Game.scheduleOnce(() => {
                                onSchedule();
                            });
                        });
                        await gameInstance.Game.onPreload();
                    }
                    if (options.onLaunch) options.onLaunch();
                    AL_GAME_HUA_CustomLoading.default.hide().then(() => {
                        gameInstance.Game.onPrepare();
                        resolve();
                    });
                });
            });
        });
    };

    /** 
     * 进入主界面方法 
     * @param {Object} options - 可选对象参数
     */
    gameInstance.jinru_zhujiemian = function (options = {}) {
        const _this = this;
        return new Promise((resolve) => {
            gameInstance.goMainCount++;
            AL_GAME_HUA_PlatformManager.PlatformManager.getInstance().wx.triggerGC();
            AL_GAME_HUA_CustomLoading.default.show().then(() => {
                cc.director.loadScene(AL_GAME_HUA_Define.Define.SCENE[1], async function () {
                    if (options.onLaunch) options.onLaunch();
                    AL_GAME_HUA_CustomLoading.default.hide().then(() => {
                        resolve();
                    });
                });
            }).catch((e)=>{
                console.log("进入主界面方法 报错啦")
                console.log(e);
            }) 
            
        });
    };

    /** 
     * 返回到主菜单方法
     */
    gameInstance.huidao_shouye = function () {
        AL_GAME_HUA_ConfigHandler.ConfigHandler.xiaohui_suoyou_peizhi_duixiang();
        if (cc.find("transition")) {
            const transitionNode = cc.find("transition");
            cc.game.removePersistRootNode(transitionNode);
        }
        gameInstance.topBar.node.destroy();
        gameInstance.topBar = null;
        internalTimerNode.node.destroy();
        internalTimerNode = null;
        AL_APP_SDK_MANAGER.subGame.huidao_shouye();
    };

    /** 
     * 检查体力是否足够
     * @param {Function} onSuccess - 体力足够的回调
     * @param {Function} onBuy - 购买体力的回调
     * @param {Function} onClose - 操作关闭的回调
     */
    gameInstance.jiancha_tili_shifou_zugou = function (onSuccess, onBuy, onClose) {
        if (AL_GAME_HUA_GameConst.GameConst.getInstance().vit_enabled) {
            if (AL_GAME_HUA_CurrencyData.CurrencyData.getInstance().getHave(AL_GAME_HUA_CurrencyData.CurrencyType.VIT) > 0) {
                AL_GAME_HUA_CurrencyData.CurrencyData.getInstance().shiyong_huobi({
                    type: AL_GAME_HUA_CurrencyData.CurrencyType.VIT,
                    num: 1,
                });
                AL_GAME_HUA_EventConfig.EventConfig.getInstance().assetsReportCost({
                    source: AL_GAME_HUA_EventConfig.AssetsSource.game,
                    action: AL_GAME_HUA_EventConfig.AssetsAction.cost,
                    goodsList: [
                        {
                            goods: AL_GAME_HUA_GoodsConfig.GoodsId.vit,
                            num: 1,
                        },
                    ],
                });
                onSuccess();
            } else {
                AL_GAME_HUA_AddPrefab.AddPrefab.chuangjian_tishi_jiedian_mianban({
                    str: "体力不足",
                });
                AL_GAME_HUA_AddPrefab.AddPrefab.create_add_vit({
                    onBuy,
                    onClose,
                });
            }
        } else {
            onSuccess();
        }
    };

    /** 
     * 创建顶部工具栏
     */
    gameInstance.chuangjian_dingbu_gongju_lan = function () {
        return new Promise(async (resolve) => {
            if (gameInstance.topBar && cc.isValid(gameInstance.topBar.node)) {
                return resolve();
            }
            const topBarPrefab = await AL_GAME_HUA_Common.Common.loadRes({
                url: "prefabs/main/topBar",
                bundle: AL_GAME_HUA_Define.Define.GAME_NAME,
                type: cc.Prefab,
            });
            const topBarNode = cc.instantiate(topBarPrefab);
            cc.director.getScene().addChild(topBarNode, 5);
            gameInstance.topBar = topBarNode.getComponent(AL_GAME_HUA_TopBar.default);
            cc.game.addPersistRootNode(topBarNode);
            topBarNode.active = false;
            gameInstance.topBar.connets = [];
            resolve();
        });
    };

    /** 
     * 显示顶部工具栏
     * @param {Object} options - 配置参数
     */
    gameInstance.xianshi_dingbu_tiao = function t(options) {
        var goldButtonEnable;
        var vitButtonEnable;
        var lifeNodeConfig;
        var lifeNodeComponent;
        var newNode;
        if (gameInstance.topBar) {
            gameInstance.topBar.com_gold_bar.node.active = options.showGold || options.showAll;
            gameInstance.topBar.com_star_bar.node.active = options.showStar || options.showAll;
            gameInstance.topBar.com_vit_bar.node.active = options.showVit || options.showAll;
            gameInstance.topBar.com_gold_bar.button.active = null === (goldButtonEnable = options.goldButtonEnable) || void 0 === goldButtonEnable || goldButtonEnable;
            gameInstance.topBar.com_vit_bar.button.active = null === (vitButtonEnable = options.vitButtonEnable) || void 0 === vitButtonEnable || vitButtonEnable;
            gameInstance.topBar.node.active = !0;
            var connects = gameInstance.topBar.connets;
            if (-1 == connects.indexOf(options.target)) {
                connects.push(options.target);
            }
            if (!AL_GAME_HUA_GameConst.GameConst.getInstance().vit_enabled) {
                gameInstance.topBar.com_vit_bar.node.active = !1;
            }
            options.target.options = options;
            lifeNodeConfig = {
                node: options.target,
                chuli_destory_hanshu: function () {
                    connects.remove(options.target);
                    connects.length > 0 ? t(connects[connects.length - 1].options) : (gameInstance.topBar.node.active = !1);
                }
            };
            (newNode = new cc.Node()).name = "XXLifeNode";
            (lifeNodeComponent = newNode.addComponent(AL_GAME_HUA_LifeNode.default)).chuli_load_hanshu = lifeNodeConfig.chuli_load_hanshu;
            lifeNodeComponent.chuli_destory_hanshu = lifeNodeConfig.chuli_destory_hanshu;
            lifeNodeComponent.chuli_enabeld_hanshu = lifeNodeConfig.chuli_enabeld_hanshu;
            lifeNodeComponent.chuli_disabled_hanshu = lifeNodeConfig.chuli_disabled_hanshu;
            lifeNodeConfig.node.addChild(newNode);
        }
    };

    // 私有变量
    let internalTimerNode = null;

    /** 
     * 创建计时器记录器
     */
    gameInstance.chuangjian_jishiqi_jiluqi = function () {
        if (!internalTimerNode || !cc.isValid(internalTimerNode)) {
            const timerNode = new cc.Node("timerRecord");
            const timerRecordComp = timerNode.addComponent(AL_GAME_HUA_TimerRecord.default);
            cc.director.getScene().addChild(timerNode);
            cc.game.addPersistRootNode(timerNode);
            internalTimerNode = timerRecordComp;
        }
    };

    /** 
     * 添加计时器
     * @param {Object} timerOptions - 计时器配置参数
     */
    gameInstance.tianjia_jishiqi = function (timerOptions) {
        if (internalTimerNode) {
            internalTimerNode.tianjia_jishiqi(timerOptions);
        }
    };
})(exports.Instance || (exports.Instance = {}));

